<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href="style/main.css" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="script/jquery-1.8.2.js"></script>
        <script type="text/javascript" src="script/js1.js"></script>
        <title></title>
    </head>
    <body>
        <header>
        <?php
        session_start();
        mysql_connect("localhost:3306", "root", "16886611");
        mysql_select_db("digitalsales");
        if(isset($_GET["action"])){
            if($_GET["action"]=="logout"){
               $_SESSION["username"]=NULL;
            }
        }
        if(!isset($_SESSION["username"])||$_SESSION["username"]==NULL){
        echo '<ul>
                <li id="login">
                  <p>Log in</p>
                </li>
                 <li>
                    <p><a href="../PhpProject1/register/register.php">Register</a></p>
                  </li>
                </ul>';
        }else{
            echo '<ul>
                <li id="logout">
                  <p><a href="../PhpProject1/index.php?action=logout">Log out</a></p>
                </li>
                 <li>
                    <p><a href="../PhpProject1/myAccount/mtAccount.php">My Account</a></p>
                  </li>
                  <li>
                    <p><a href="../PhpProject1/myAccount/order.php">My Orders</a></p>
                  </li>
                  <li>
                    <p>Welcome back,'.$_SESSION["username"].'!</p>
                  </li>
                </ul>';
        }
        
        ?>
        </header>
        <div id="Mainpanel">
            <div id="logo">
                <!--
                <div id="test"></div>
                <button onclick="insertBlock('hello')" type="button">Add Block</button>
                <button onclick="jQueryHide('test')" type="button">Hide Text</button>
                -->
                <a><img src="images/log.png"></a>
            </div>
            <div id="search">
                <form name="form2" method="GET" action="../PhpProject1/search.php">
                    <input name="text" type="text" class="textArea" />
                    <input type="submit" class="searchButton" value="Search" /><br/>
                </form>
            </div>
            <div style="clear:both;"></div>
        </div>
        <div id="Mainpanel2">
            <div id="List" >
            <div id="Categories" onmouseleave="hideCurrentList()">
                <div id="big">
                <table cellpadding="0" cellspacing="0">
                    <thead><tr><th>Categories</th></tr></thead>
                    <tbody>
                        <tr>
                            <td id="item1" ><a href="../PhpProject1/search.php?category=FOOD">FOOD</a></td>
                        </tr>
                        <tr>
                            <td id="item2" ><a href="../PhpProject1/search.php?category=MACHINE">MACHINE</a></td>
                        </tr>
                        <tr>
                            <td id="item3" ><a href="../PhpProject1/search.php?category=MEDICINE">MEDICINE</a></td>
                        </tr>
                        <tr>
                            <td id="item4" ><a href="../PhpProject1/search.php?category=BOOKS">BOOKS</a></td>
                        </tr>
                        <tr>
                            <td id="item5" ><a href="../PhpProject1/search.php?category=TOOLS">TOOLS</a></td>
                        </tr>
                        <tr>
                            <td id="item6" ><a href="../PhpProject1/search.php?category=CLOTHES">CLOTHES</a></td>
                        </tr>
                    </tbody>
                </table> 
                <table cellpadding="0" cellspacing="0">
                    <thead><tr><th>Stores</th></tr></thead>
                    <tbody>
                        <tr>
                            <td id="region1" ><a href="../PhpProject1/search.php?region=PITTSBURGH">PITTSBURGH</a></td>
                        </tr>
                        <tr>
                            <td id="region2" ><a href="../PhpProject1/search.php?region=PHILADELPHIA">PHILADELPHIA</a></td>
                        </tr>
                        <tr>
                            <td id="region3" ><a href="../PhpProject1/search.php?region=YORK">YORK</a></td>
                        </tr>
                        <tr>
                            <td id="region4" ><a href="../PhpProject1/search.php?region=WILLIAMPORT">WILLIAMPORT</a></td>
                        </tr>
                        <tr>
                            <td id="region5" ><a href="../PhpProject1/search.php?region=SCRANION">SCRANTON</a></td>
                        </tr>
                    </tbody>
                </table> 
                </div>
                <div id="small" class="small">
                    <?PHP 
                    $TYPE="FOOD";
                    echo '<h2>'.$TYPE.'</h2>';
                    $sql3="select product_type from product_types where parent_type='".$TYPE."'";
                    $result3=mysql_query($sql3);
                     while ($res3=mysql_fetch_row($result3)){
                         echo '<a href="../PhpProject1/search.php?category='.$res3[0].'">'.$res3[0].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small1" class="small">
                    <?PHP 
                    $TYPE2="MACHINE";
                    echo '<h2>'.$TYPE2.'</h2>';
                    $sql4="select product_type from product_types where parent_type='".$TYPE2."'";
                    $result4=mysql_query($sql4);
                     while ($res4=mysql_fetch_row($result4)){
                         echo '<a href="../PhpProject1/search.php?category='.$res4[0].'">'.$res4[0].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small2" class="small">
                    <?PHP 
                    $TYPE3="MEDICINE";
                    echo '<h2>'.$TYPE3.'</h2>';
                    $sql5="select product_type from product_types where parent_type='".$TYPE3."'";
                    $result5=mysql_query($sql5);
                     while ($res5=mysql_fetch_row($result5)){
                         echo '<a href="../PhpProject1/search.php?category='.$res5[0].'">'.$res5[0].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small3" class="small">
                    <?PHP 
                    $TYPE4="BOOKS";
                    echo '<h2>'.$TYPE4.'</h2>';
                    $sql6="select product_type from product_types where parent_type='".$TYPE4."'";
                    $result6=mysql_query($sql6);
                     while ($res6=mysql_fetch_row($result6)){
                         echo '<a href="../PhpProject1/search.php?category='.$res6[0].'">'.$res6[0].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small4" class="small">
                    <?PHP 
                    $TYPE5="TOOLS";
                    echo '<h2>'.$TYPE5.'</h2>';
                    $sql7="select product_type from product_types where parent_type='".$TYPE5."'";
                    $result7=mysql_query($sql7);
                     while ($res7=mysql_fetch_row($result7)){
                         echo '<a href="../PhpProject1/search.php?category='.$res7[0].'">'.$res7[0].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small5"class="small" >
                    <?PHP 
                    $TYPE6="CLOTHES";
                    echo '<h2>'.$TYPE6.'</h2>';
                    $sql8="select product_type from product_types where parent_type='".$TYPE6."'";
                    $result8=mysql_query($sql8);
                     while ($res8=mysql_fetch_row($result8)){
                         echo '<a href="../PhpProject1/search.php?category='.$res8[0].'">'.$res8[0].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small6" class="small">
                    <?PHP 
                    $region1="PITTSBURGH";
                    echo '<h2>BRANCHES IN '.$region1.'</h2>';
                    $sql9="select storeid,name from stores where region='".$region1."'";
                    $result9=mysql_query($sql9);
                     while ($res9=mysql_fetch_row($result9)){
                         echo '<a href="../PhpProject1/store.php?storeid='.$res9[0].'">'.$res9[1].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small7" class="small">
                    <?PHP 
                    $region2="PHILADELPHIA";
                    echo '<h2>BRANCHES IN '.$region2.'</h2>';
                    $sql10="select storeid,name from stores where region='".$region2."'";
                    $result10=mysql_query($sql10);
                     while ($res10=mysql_fetch_row($result10)){
                         echo '<a href="../PhpProject1/store.php?storeid='.$res10[0].'">'.$res10[1].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small8" class="small">
                    <?PHP 
                    $region3="YORK";
                    echo '<h2>BRANCHES IN '.$region3.'</h2>';
                    $sql11="select storeid,name from stores where region='".$region3."'";
                    $result11=mysql_query($sql11);
                     while ($res11=mysql_fetch_row($result11)){
                         echo '<a href="../PhpProject1/store.php?storeid='.$res11[0].'">'.$res11[1].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small9" class="small">
                    <?PHP 
                    $region4="WILLIAMPORT";
                    echo '<h2>BRANCHES IN '.$region4.'</h2>';
                    $sql12="select storeid,name from stores where region='".$region4."'";
                    $result12=mysql_query($sql12);
                     while ($res12=mysql_fetch_row($result12)){
                         echo '<a href="../PhpProject1/store.php?storeid='.$res12[0].'">'.$res12[1].'</a><br/>';
                     }
                    ?>
                </div>
                <div id="small10" class="small">
                    <?PHP 
                    $region5="SCRANTON";
                    echo '<h2>BRANCHES IN '.$region5.'</h2>';
                    $sql13="select storeid,name from stores where region='".$region5."'";
                    $result13=mysql_query($sql13);
                     while ($res13=mysql_fetch_row($result13)){
                         echo '<a href="../PhpProject1/store.php?storeid='.$res13[0].'">'.$res13[1].'</a><br/>';
                     }
                    ?>
                </div>
                <div style="clear:both;"></div>
            </div>
            </div>
            <div id="description">
                <div id="AddSelect">
                    <div id="pic1" class="Additem">1</div>
                    <div id="pic2" class="Additem" style="background-color:  #f59d56">2</div>
                    <div id="pic3" class="Additem">3</div>
                    <div style="clear:both;"></div>
                </div>
            </div>
            <div style="clear:both;"></div>
            <?php
            if(isset($_SESSION["username"])&&$_SESSION["username"]!=NULL){
                $sql3="select p.PRODUCT_TYPE from transactions t,products p where t.CUSTOMER='".$_SESSION["username"]."' and t.product=p.productid
                    GROUP BY p.PRODUCT_TYPE order by count(*) desc limit 0,1";
                $result3=mysql_query($sql3);
                $num3=mysql_num_rows($result3);
                if($num3>0){
                    echo '<div id="recommendList">
                       <h2>You may be interested in the following products</h2>';
                    $res3=mysql_fetch_row($result3);
                    $sql4="select p2.productid,p2.name,p2.picture,sum(quantity) from (select p1.productid,p1.name
                             ,p1.picture from (select pt1.product_type from product_types pt1 where pt1.product_type='".$res3[0]."' or pt1.parent_type='".$res3[0]."') types,
                             products p1 where p1.product_type=types.product_type) p2,transactions where p2.productid=product group by p2.productid,p2.name,p2.picture 
                             order by sum(quantity) desc limit 0,5";
                    $result4=mysql_query($sql4);
                    while ($res4=mysql_fetch_row($result4)){
                          $arg2="item/item.php?pid=".$res4[0]."";
                          echo '<div class="item" onclick="jump(\''.$arg2.'\')">
                                   <a herf="../PhpProject1/item/item.php?pid='.$res4[0].'">
                                     <div class="pic">
                                       <img src="'.$res4[2].'"/>
                                     </div>
                                    <div class="des">
                                       <p>'.$res4[1].'</p>
                                    </div>
                                    </a>
                               </div>';
                      }
                    echo '</div>';
                }
            }
            ?>
            <div id="hotList">
                <?php
                  $sql="select product_type from product_types where parent_type is null;";
                  $result=mysql_query($sql);
                  $num=mysql_num_rows($result);
                  while ($res=mysql_fetch_row($result)){
                      echo "<div class='sublist'>";
                      echo '<h2>Best Sellers in '.$res[0].'</h2>';
                      $sql2="select p2.productid,p2.name,p2.picture,sum(quantity) from (select p1.productid,p1.name
                             ,p1.picture from (select pt1.product_type from product_types pt1 where pt1.product_type='".$res[0]."' or pt1.parent_type='".$res[0]."') types,
                             products p1 where p1.product_type=types.product_type) p2,transactions where p2.productid=product group by p2.productid,p2.name,p2.picture 
                             order by sum(quantity) desc limit 0,5";
                      $result2=mysql_query($sql2);
                      while ($res2=mysql_fetch_row($result2)){
                          $arg1="item/item.php?pid=".$res2[0]."";
                          echo '<div class="item" onclick="javascript:jump(\''.$arg1.'\')">
                                   <a herf="../PhpProject1/item/item.php?pid='.$res2[0].'">
                                     <div class="pic">
                                       <img src="'.$res2[2].'"/>
                                     </div>
                                    <div class="des">
                                       <p>'.$res2[1].'</p>
                                    </div>
                                    </a>
                               </div>';
                      }
                      echo '<div style="clear:both;"></div>';
                      echo '</div>';
                  }
                  mysql_free_result($result);
                  mysql_free_result($result2);
                ?>
                
            </div>
        </div>
       
        
        <div id="popupContact">  
         <a id="popupContactClose">x</a>  
         <h1>User Log in</h1>  
         <form id="form1" name="form1" method="POST" action=""style=" margin-left: 30%">
            <label>Username:
                <input name="username" type="TEXT" id="name">
            </label><br/>
            <label>Password:
                <input name="password" type="password" id="password">
            </label><br/>
            <input type="submit" name="submit" value="Log in">
        </form>
        </div>  
    <div id="backgroundPopup"></div>
    
    <?php
    if(isset ($_POST["username"]) and $_POST["username"]!=""and isset ($_POST["password"]) and $_POST["password"]!=""){
            $usr=$_POST["username"];
            $pass=$_POST["password"];
            $sql="select * from `customers` where `account`='".$usr."' and `password`='".md5($pass)."';";
            $result=mysql_query($sql);
            $num=mysql_num_rows($result);
            mysql_free_result($result);
            if($num==0){
                echo '<script>alert("Invalid username or password!")</script>';
                echo '<meta http-equiv="refresh" content="0;url=../PhpProject1/index.php" />';
            }else{
                $_SESSION["username"]=$usr;
                echo '<meta http-equiv="refresh" content="0;url=../PhpProject1/index.php" />';
            }
        }
    ?>

    </body>
</html>
